package com.takeshequ.main;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.korovyansk.android.slideout.SlideoutActivity;
import com.takeshequ.BasicFragmentActivity;
import com.takeshequ.R;
import com.takeshequ.SMApplication;
import com.takeshequ.conversation.ConversationFragment;
import com.takeshequ.feeds.FeedsFragment;
import com.takeshequ.friends.FriendsFragment;
import com.takeshequ.nearest.NearestFragment;
import com.takeshequ.nearest.SiftPopupWindow;

public class MainFragmentActivity extends BasicFragmentActivity {
	private static final String TAG = "MainFragmentActivity";
	private Resources resources;
	private Context context;

	private ViewPager mPager;
	private ArrayList<Fragment> fragmentsList;
	private ImageButton slideBtn;
	private TextView mainTitleView;
	private ImageView rightBtn;
	private SiftPopupWindow menuWindow;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		resources = getResources();
		context = MainFragmentActivity.this;
		initSlideBtn();
		initViewPager();
	}

	@Override
	protected void onResume() {
		super.onResume();
		// initUserInformation();
		rightBtn = (ImageView) findViewById(R.id.main_right_btn);

	}

	private void initUserInformation() {
		// tvUserName.setText(sharedPreferencesUtils.getNickName());
	}

	private void initSlideBtn() {
		// tvUserName = (TextView) findViewById(R.id.textView1);
		slideBtn = (ImageButton) findViewById(R.id.main_slide_btn);
		slideBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				int width = (int) TypedValue.applyDimension(
						TypedValue.COMPLEX_UNIT_DIP, 100, getResources()
								.getDisplayMetrics());
				SlideoutActivity.prepare(MainFragmentActivity.this,
						R.id.inner_content, width);
				startActivity(new Intent(MainFragmentActivity.this,
						MenuActivity.class));
				overridePendingTransition(0, 0);
			}
		});
	}

	private void initViewPager() {
		mPager = (ViewPager) findViewById(R.id.main_center_vPager);
		fragmentsList = new ArrayList<Fragment>();

		NearestFragment nearestFragment = NearestFragment.newInstance(context,
				((SMApplication) this.getApplication()));
		Fragment talkListFragment = ConversationFragment.newInstance(context,
				((SMApplication) this.getApplication()));
		Fragment friendsFragment = FriendsFragment.newInstance(context, mPager);
		Fragment feedsFragment = FeedsFragment.newInstance(context,
				((SMApplication) this.getApplication())); // TalkFragment.newInstance(context,
		// mPager);
		fragmentsList.add(friendsFragment);
		fragmentsList.add(feedsFragment);
		fragmentsList.add(nearestFragment);
		fragmentsList.add(talkListFragment);
		mPager.setAdapter(new MyFragmentPagerAdapter(
				getSupportFragmentManager(), fragmentsList));
		mPager.setCurrentItem(1);
		mainTitleView = (TextView) findViewById(R.id.main_title);
		mainTitleView.setText("好友动态");
		mPager.setOffscreenPageLimit(fragmentsList.size());
		((SMApplication) this.getApplication()).viewPager = mPager;
		mPager.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return false;
			}
		});
		mPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				if (arg0 == 0) {
					mainTitleView.setText("联系人");
					rightBtn.setVisibility(View.GONE);
				} else if (arg0 == 1) {
					mainTitleView.setText("好友动态");
					rightBtn.setVisibility(View.GONE);
				} else if (arg0 == 2) {
					mainTitleView.setText("附近的人");
					rightBtn.setVisibility(View.VISIBLE);
					rightBtn.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							menuWindow = new SiftPopupWindow(
									MainFragmentActivity.this, fragmentsList
											.get(2));
							popupHandler.sendEmptyMessageDelayed(0, 100);

						}
					});
				} else if (arg0 == 3) {
					mainTitleView.setText("会话");
					rightBtn.setVisibility(View.GONE);
				}
				((BasicFragment) fragmentsList.get(arg0)).show();
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	private Handler popupHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				menuWindow.showAtLocation(MainFragmentActivity.this
						.findViewById(R.id.inner_content), Gravity.TOP
						| Gravity.RIGHT, 0, 0);
				menuWindow.update();
				break;
			}
		}

	};

}
